package com.heitian.ssm.controller;

import com.google.gson.JsonObject;
import com.heitian.ssm.model.Linkman;
import com.heitian.ssm.model.PageBean;
import com.heitian.ssm.service.LinkmanService;
import com.heitian.ssm.utils.CommonUtils;
import com.heitian.ssm.utils.GsonUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者： 陈建峰
 * 创建时间： 17/10/18 下午1:13
 * 版本： [1.0, 17/10/18]
 * 描述： <描述>
 */
@Controller
@RequestMapping("/Linkman")
public class LinkmanController {

    private Logger log = Logger.getLogger(LinkmanController.class);

    @Resource
    LinkmanService linkmanService;

    /**
     * 保存
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/saveLinkman", method = RequestMethod.POST)
    public void saveLinkman(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        Linkman linkman = new Linkman();
        if (paras.has("linkmanName")) {
            linkman.setLinkmanName(paras.get("linkmanName").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("linkmanHeadUrl")) {
            linkman.setLinkmanHeadUrl(paras.get("linkmanHeadUrl").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("linkmanSex")) {
            linkman.setLinkmanSex(paras.get("linkmanSex").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("linkmanAddress")) {
            linkman.setLinkmanAddress(paras.get("linkmanAddress").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("linkmanTelephone")) {
            linkman.setLinkmanTelephone(paras.get("linkmanTelephone").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("linkmanCompany")) {
            linkman.setLinkmanCompany(paras.get("linkmanCompany").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("linkmanQQ")) {
            linkman.setLinkmanQQ(paras.get("linkmanQQ").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("linkmanEmail")) {
            linkman.setLinkmanEmail(paras.get("linkmanEmail").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("classifyGuid")) {
            linkman.setClassifyGuid(paras.get("classifyGuid").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        linkman.setUserGuid("");
        linkman.setIsTelephoneCanSee("0");
        linkman.setLinkmanGuid(CommonUtils.GetUUID());
        linkmanService.saveLinkman(linkman);
        response.getWriter().write(CommonUtils.createJson("1", "添加成功！", null).toString());
    }

    /**
     * 删除
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/deleteLinkman", method = RequestMethod.POST)
    public void deleteLinkman(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String linkmanGuids = paras.get("linkmanGuids").getAsString();
        String[] linkmanGuid = linkmanGuids.split(";");
        for (int i = 0; i < linkmanGuid.length; i++) {
            linkmanService.deleteLinkman(linkmanGuid[i]);
        }
        response.getWriter().write(CommonUtils.createJson("1", "删除成功！", null).toString());
    }

    /**
     * 获取数量
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllLinkmanCountByclassifyGuid", method = RequestMethod.POST)
    public void getAllLinkmanCountByclassifyGuid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String classifyGuid = paras.get("classifyGuid").getAsString();
        int count = linkmanService.getAllLinkmanCountByclassifyGuid(classifyGuid);
        Map<String,String> map = new HashMap<String, String>();
        map.put("count",count+"");
        String UserArea = GsonUtil.GsonString(map);
        response.getWriter().write(CommonUtils.createJson("1", "", UserArea).toString());
    }

    /**
     * 获取文件
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getLinkmanByGuid", method = RequestMethod.POST)
    public void getLinkmanByGuid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String linkmanGuid = paras.get("linkmanGuid").getAsString();
        Linkman linkman = linkmanService.getLinkmanByGuid(linkmanGuid);
        String UserArea = null;
        String code = "0";
        String msg = "";
        if (linkman != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(linkman);
        }else{
            msg = "未找到联系人";
        }
        response.getWriter().write(CommonUtils.createJson(code, msg, UserArea).toString());
    }

    /**
     * 分页获取文件数据
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllLinkmanByclassifyGuid", method = RequestMethod.POST)
    public void getAllLinkmanByclassifyGuid(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String classifyGuid = paras.get("classifyGuid").getAsString();
        String index = paras.get("index").getAsString();
        String currentPageCount = paras.get("currentPageCount").getAsString();
        PageBean<Linkman> pageBean = linkmanService.getAllLinkmanByclassifyGuid(classifyGuid,Integer.parseInt(index),Integer.parseInt(currentPageCount));
        String UserArea = null;
        String code = "0";
        String message = "";
        if (pageBean != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(pageBean);
        } else {
            message = "用户数据获取失败！";
        }
        response.getWriter().write(CommonUtils.createJson(code, message, UserArea).toString());
    }

    /**
     * 获取文件所有数量
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllLinkmanCount", method = RequestMethod.POST)
    public void getAllLinkmanCount(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        int count = linkmanService.getAllLinkmanCount();
        Map<String,String> map = new HashMap<String, String>();
        map.put("count",count+"");
        String UserArea = GsonUtil.GsonString(map);
        response.getWriter().write(CommonUtils.createJson("1", "", UserArea).toString());
    }

}
